/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.binary.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import offset.nodes.Constants;
import offset.nodes.server.controller.AuthenticatingAction;
import offset.nodes.server.dispatch.controller.DispatchAttribute;
import offset.nodes.server.model.RepositoryModel;
import offset.nodes.server.view.css.CssInputStream;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Walter Lütgenau
 */
public class BinaryAction extends AuthenticatingAction {
    
    /** Creates a new instance of DocumentationAction */
    public BinaryAction() {
    }
    
    public ActionForward process(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ActionForward forward = mapping.findForward("binary");
        DispatchAttribute dispatchInfo = (DispatchAttribute) request.getAttribute(DispatchAttribute.ATT_DISPATCH);
        if ( dispatchInfo == null || dispatchInfo.getNode() == null)
            return forward;
        
        RepositoryModel model = new RepositoryModel(getRepositorySession(request));
        
        BinaryAttribute downloadInfo = model.getDownloadInfo(dispatchInfo.getNode());

        if ( downloadInfo.getContentType() != null 
                && downloadInfo.getContentType().equals(Constants.MIMETYPE_CSS)) {
            downloadInfo.setInput(new CssInputStream(request, downloadInfo.getInput()));
        }
        
        request.setAttribute(BinaryAttribute.ATT_DOWNLOAD, downloadInfo );
        return forward;
    }
    
    
}
